package com.misc;

import com.type.ByteArrayParcelable;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Tool {
	static final int MILLISECOND = 150;
	static final int NUM = 512;

	
	public static void sendByteArray(DataOutputStream dos, byte[] byteArray) throws IOException, InterruptedException {
		dos.writeInt(byteArray.length);
		
		int currentPos = 0;
		int byteNumToCopy = 0;
		while (currentPos != byteArray.length) {
			byteNumToCopy = (byteArray.length-currentPos >= NUM) ? NUM : byteArray.length-currentPos;
			byte[] tempByteArray = new byte[byteNumToCopy];
			System.arraycopy(byteArray, currentPos, tempByteArray, 0, byteNumToCopy);
			currentPos += byteNumToCopy;
			dos.write(tempByteArray, 0, byteNumToCopy);
		}
		Thread.sleep(MILLISECOND);
	}

	
	public static byte[] receiveByteArray(DataInputStream dis) throws IOException, InterruptedException {
		byte[] byteArray = new byte[dis.readInt()];
		
		int currentPos = 0;
		int byteNumToCopy = 0;
		while (currentPos != byteArray.length) {
			byteNumToCopy = (byteArray.length-currentPos >= NUM) ? NUM : byteArray.length-currentPos;
			byte[] tempByteArray = new byte[byteNumToCopy];
			dis.read(tempByteArray, 0, byteNumToCopy);
			System.arraycopy(tempByteArray, 0, byteArray, currentPos, byteNumToCopy);
			currentPos += byteNumToCopy;
		}
		Thread.sleep(MILLISECOND);
		
		return byteArray;
	}
	
	
	public static byte[] bitmap2ByteArray(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
	
	
	public static Bitmap byteArray2Bitmap(byte[] byteArray) {
		return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
	}
	
	
	public static Bitmap byteArrayParcelable2Bitmap(ByteArrayParcelable byteArrayParcelable) {
		return byteArray2Bitmap(byteArrayParcelable.getByteArray());
	}


	public static void flipZ(int[] dst, int[] src, int depth, int pos, boolean direction, int srcW, int srcH) {
		int startX, startY;
		int scaledWidth, scaledHeight;
		int posX, posY;
		int counterX, counterY;
		int halfSrcWidth;
		int srcWidth = srcW;
		int srcHeight = srcH;
		int dstWidth = srcW;
		int dstHeight = srcH + (depth << 1);
		halfSrcWidth = srcWidth >> 1;
		startX = halfSrcWidth - pos;
		int startOffsetY = startX * depth / halfSrcWidth;
		int doubleStartOffsetY = startOffsetY << 1;
	
		scaledWidth = pos << 1;
		counterX = 0;
		int offsetYCounter = 0;
		startY = direction ? depth + startOffsetY : depth - startOffsetY;
		posX = 0;
		int x = 0;
		int y;
		int temp;
	
		for (x = 0; x < scaledWidth; ++x) {
			counterY = 0;
			posY = 0;
			temp = startY * dstWidth + startX + x;
			scaledHeight = dstHeight - (startY << 1);
			for (y = 0; y < scaledHeight; ++y) {
				counterY += srcHeight;
				dst[temp + y * dstWidth] = src[posY * srcWidth + posX];
				posY = (counterY / scaledHeight);
			}
			offsetYCounter += doubleStartOffsetY;
			if (offsetYCounter >= scaledWidth) {
				offsetYCounter -= scaledWidth;
				startY = direction ? --startY : ++startY;
			}
			counterX += srcWidth;
			posX = (counterX / scaledWidth);
		}
	}
}
